unit Work_With_SECURITIES;
interface

uses
    TEClientLib_TLB, consts_Unit, Classes;

type
  sectbl = record
    ID: integer;
    SecBoard, SecCode, SecName, ShortName, TradingStatus: WideString;
    LotSize, Decimals: integer;
    cur_time,ExecTerm:WideString;
    SellDeposit,BuyDeposit:Double;
    Last:Double;
    Quantity:Double;
    Value:Double;
    OpenPositions:Double;

  end;

type
    TSECURITIES = class(TObject)
        private
            SlotTable:TSlotTable;
            SlotTableReplied:Boolean;
            SlotTableOpenedF:Boolean;
            OId:Integer;
            secList:array of SecTbl;
            st:TStringList;
            FOnReplBegin:TNotifyEvent;
            FOnReplied:TNotifyEvent;
            procedure SlotTableAddRow(ASender: TObject; OpenID, RowID: Integer; Fields: OleVariant);
            procedure SlotTableUpdRow(ASender: TObject; OpenID, RowID: Integer; Fields: OleVariant);
            procedure SlotTableReplBegin(ASender: TObject; OpenID: Integer);
            procedure SlotTableReplEnd(ASender: TObject; OpenID: Integer);
            procedure SlotTableOpened(ASender: TObject; OpenID: Integer);
            procedure SlotTableClosed(ASender: TObject; OpenID: Integer);
        public
            CurSec:SecTbl;
            constructor Create;
            destructor Free;
            procedure getAllSecurities(sltId:Word);
            procedure getSecParams(ID:Word);
            procedure getLastPriceAndVol(ID:Word);
            function SearchFINum(ASecBoard,ASecCode:WideString):Integer;
            property Replied:Boolean read SlotTableReplied;
            property FIList:TStringList read st;
            property OnReplBegin:TNotifyEvent read FOnReplBegin write FOnReplBegin;
            property OnReplied:TNotifyEvent read FOnReplied write FOnReplied;


    end;

implementation

function TSECURITIES.SearchFINum(ASecBoard,ASecCode:WideString):Integer;
var
    i:Integer;
begin
    Result:=-1;
    for i:=Low(secList) to High(secList) do
    begin
        if (ASecBoard=secList[i].SecBoard)and(ASecCode=secList[i].SecCode) then
        begin
            Result:=i;
            Break;
        end;
    end;
end;

procedure TSECURITIES.SlotTableAddRow(ASender: TObject; OpenID, RowID: Integer; Fields: OleVariant);
var
    h:Int64;
begin

    SetLength(secList,Length(secList)+1);
    h:=High(secList);
//    0        1       2       3         4             5       6        7    8        9         10    11       12    13            14
//  'ID,SecBoard,SecCode,SecName,ShortName,TradingStatus,LotSize,Decimals,Time,ExecTerm,SellDeposit,Last,Quantity,Value,OpenPositions'
    secList[h].ID:=Fields[0];
    secList[h].SecBoard:=Fields[1];
    secList[h].SecCode:=Fields[2];
    secList[h].SecName:=Fields[3];
    secList[h].ShortName:=Fields[4];
    secList[h].LotSize:=Fields[6];
    secList[h].Decimals:=Fields[7];
    secList[h].ExecTerm:=Fields[9];
    secList[h].SellDeposit:=Fields[10];
    secList[h].Last:=Fields[11];
    secList[h].Quantity:=Fields[12];
    secList[h].Value:=Fields[13];
    secList[h].OpenPositions:=Fields[14];

end;

procedure TSECURITIES.SlotTableUpdRow(ASender: TObject; OpenID, RowID: Integer; Fields: OleVariant);
var
    h:Int64;
begin

    h:=RowID;
//    0        1       2       3         4             5       6        7    8        9         10    11       12    13            14
//  'ID,SecBoard,SecCode,SecName,ShortName,TradingStatus,LotSize,Decimals,Time,ExecTerm,SellDeposit,Last,Quantity,Value,OpenPositions'
    if (h<High(secList)) then
    begin
        if (secList[h].SecName = Fields[3])and(CurSec.SecCode = Fields[2]) then
        begin
            if Assigned(FOnReplBegin) then FOnReplBegin(Self);
            CurSec.Last:=Fields[11];
            CurSec.Quantity:=Fields[12];
            CurSec.Value:=Fields[13];
            CurSec.OpenPositions:=Fields[14];
            CurSec.TradingStatus:=Fields[5];
            if Assigned(FOnReplied) then FOnReplied(Self);
        end;
    end;

end;

procedure TSECURITIES.SlotTableReplBegin(ASender: TObject; OpenID: Integer);
begin
//    SetLength(secList,0);
    SlotTableReplied:=False;
    if Assigned(FOnReplBegin) then FOnReplBegin(Self);
end;

procedure TSECURITIES.SlotTableReplEnd(ASender: TObject; OpenID: Integer);
var
    i:Word;
begin

    if (st.Count<1)and(High(secList)>0) then
    begin
        for i:=Low(secList) to High(secList) do
        begin
            st.Add(secList[i].ShortName);
        end;
    end;
    SlotTableReplied:=True;
//    SlotTable.Close(OId);
    if Assigned(FOnReplied) then FOnReplied(Self);
end;
procedure TSECURITIES.SlotTableOpened(ASender: TObject; OpenID: Integer);
begin
    SlotTableOpenedF:=True;
    OId:=OpenID;
end;
procedure TSECURITIES.SlotTableClosed(ASender: TObject; OpenID: Integer);
begin
    SlotTableOpenedF:=False;
end;

constructor TSECURITIES.Create;
begin
    SlotTableReplied:=False;
    SlotTableOpenedF:=False;
    SlotTable:=TSlotTable.Create(nil);
    SlotTable.OnAddRow:=SlotTableAddRow;
    SlotTable.OnUpdateRow:=SlotTableUpdRow;
    SlotTable.OnReplEnd:=SlotTableReplBegin;
    SlotTable.OnReplEnd:=SlotTableReplEnd;
    SlotTable.OnOpened:=SlotTableOpened;
    SlotTable.OnClosed:=SlotTableClosed;
    SetLength(secList,0);
    st:=TStringList.Create;
    st.Clear;
    inherited;
end;

destructor TSECURITIES.Free;
begin
    SlotTable.Close(OId);
    st.Free;
    SlotTable.Free;
//    inherited Free;    
//
end;

procedure TSECURITIES.getAllSecurities(sltId:Word);
begin
    SlotTable.FieldNames := fnames[1];
    SlotTable.Open(sltId, tname[1], '');
end;

procedure TSECURITIES.getSecParams(ID:Word);
begin
    CurSec.ID:=SecList[ID].ID;
    CurSec.SecBoard:=SecList[ID].SecBoard;
    CurSec.SecCode:=SecList[ID].SecCode;
    CurSec.ShortName:=SecList[ID].ShortName;
    CurSec.LotSize:=SecList[ID].LotSize;
    CurSec.Decimals:=SecList[ID].Decimals;
    CurSec.ExecTerm:=SecList[ID].ExecTerm;
    CurSec.SellDeposit:=SecList[ID].SellDeposit;
end;

procedure TSECURITIES.getLastPriceAndVol(ID:Word);
begin
    CurSec.Last:=SecList[ID].Last;
    CurSec.Quantity:=SecList[ID].Quantity;
    CurSec.Value:=SecList[ID].Value;
end;

end.
